/*
 * Copyright (c) 2010, Nikolaus Moll
 * All rights reserved.
 */
package net.miblounge.gradecalculator.plugin.core.util;

import java.util.LinkedList;
import java.util.List;

import net.miblounge.gradecalculator.core.descriptors.StudyRegulationsDescription;
import net.miblounge.gradecalculator.core.descriptors.courses.Course;
import net.miblounge.gradecalculator.core.descriptors.courses.CourseDescription;
import net.miblounge.gradecalculator.core.enums.ExaminationType;
import net.miblounge.gradecalculator.core.enums.ExaminationTypeList;
import net.miblounge.gradecalculator.core.student.ExaminationResult;
import net.miblounge.gradecalculator.core.student.ExaminationResultType;
import net.miblounge.gradecalculator.importplugin.data.ImportedExaminationResult;
import net.miblounge.gradecalculator.importplugin.data.ImportedExaminationResult.ResultState;

public final class ResultConvert {
	private ResultConvert() {
	}
	
    public static ExaminationResult convertImportedResult(final StudyRegulationsDescription spod,
            final ImportedExaminationResult importedResult)
    {
        final CourseDescription description = new CourseDescription();
        description.setName(importedResult.getCourseName());
        description.setECTS(importedResult.getEcts());

        final ExaminationResult result = new ExaminationResult();
        result.setCourseDescription(description);
        result.setCourseIdentifier(importedResult.getCourseName());
        try {
            final int examNumber = Integer.parseInt(importedResult.getExaminationResultId());
            result.setExamNumber(examNumber);
            final Course course = spod.getCourseByExamNumber(examNumber);

            // TODO improve, remove exception here
            result.setCourseIdentifier(course.getIdentifier());
        }
        catch (final Exception e) {
            // failed to assign by examination id
            final List<Course> allCoursesByName = spod.getCourseByName(importedResult.getCourseName());
            final List<Course> courses = new LinkedList<Course>();
            for (final Course course : allCoursesByName) {
                if (Math.abs(course.getECTS() - importedResult.getEcts()) > 0.01) {
                    continue;
                }

                courses.add(course);
            }

            if (courses.size() == 1) {
                final Course course = courses.get(0);
                result.setCourseIdentifier(course.getIdentifier());
            }
        }

        if (importedResult.getState() == ResultState.PASSED) {
            result.setUngradedPassed(true);
            if (importedResult.getGrade().equals("")) {
            	if (ExaminationType.values().length > 0) {
            		final ExaminationTypeList list = new ExaminationTypeList();
            		list.add(ExaminationType.values()[0]);
            		description.setUngradedPerformance(list);
            	}
            }
            else {
            	if (ExaminationType.values().length > 0) {
            		final ExaminationTypeList list = new ExaminationTypeList();
            		list.add(ExaminationType.values()[0]);
            		description.setGradedPerformance(list);
            	}
                result.setGrade(ExaminationResultType.PASSED, importedResult.getGrade());
            }
        }

        return result;
    }

}

